// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    future: [{
      id: 1,
      weather: '晴',
      data: '2021-12-15'
    },{
      id: 2,
      weather: '阴',
      data: '2021-12-16' 
    },{
      id: 3,
      weather: '雨',
      data: '2021-12-17'
    }],
    date: '2021-12-14',
    todayWeather: '多云',
    todayTem: '8~15',
    todayWindy: '东北风/2级',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  onLoad() {
    let that = this
    wx.request({
      url: 'http://localhost:8082/user/weather',
      dataType: 'json',
      method: 'get',
      data: {},
      success(res) {
        let future = res.data.result.future
        that.setData({
          future: future
        })
        console.dir(res)
      },
      fail(res) {
        console.dir(res)
      }
    })

    // setTimeout(()=> {
    //   let tempData = this.data.future
    //   this.data.future.forEach(item=>{
    //     tempData.push(item)
    //   })
    //   this.setData({
    //     future: tempData
    //   })
    //   this.setData({
    //     todayWeather: '雨'
    //   })
    //   this.setData({
    //     todayWeather: '雨'
    //   })
    // },2000)

    wx.login({
      success (res) {
        if (res.code) {
          console.dir("code" + res.code)
          // return
          //发起网络请求
          wx.request({
            url: 'http://localhost:8082/user/valid',
            dataType:"json",
            method:"post",
            data: {
              code: res.code
            },
            success(res) {
              if(!res.data.isValid) {
                wx.navigateTo({
                  url: '/pages/login/login?openId=' + res.data.openId,
                })
              } else {
                wx.showToast({
                  title: '欢迎你' + res.data.info.name
                })
              }
            },
            fail(res) {
              console.dir(res)
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
})